#include "ticketing.h"
#include "login.h"
#include "myfont.h"
#include <time.h>
#include "admin.h"

static char current_selected_movie[128] = {0};
static char current_selected_session[128] = {0};

//获取电影信息
bool read_movie_file(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (!fp) {
        printf("无法打开文件: %s\n", path);
        return false;
    }

    // 清空缓冲区
    memset(file_content, 0, sizeof(file_content));
    
    char line[128];
    bool first_line = true;
    
    while (fgets(line, sizeof(line), fp)) {
        // 移除行尾换行符
        line[strcspn(line, "\n")] = '\0';
        
        if (!first_line) {
            strcat(file_content, "\n"); // 添加选项分隔符
        } else {
            first_line = false;
        }
        
        strcat(file_content, line);
    }
    
    fclose(fp);
    return true;
}
//获取用户票据信息
bool read_user_movie(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (!fp) {
        printf("无法打开文件: %s\n", path);
        return false;
    }
 
    memset(file_content, 0, sizeof(file_content));
    char line[256];
    bool first_line = true;
 
     while (fgets(line, sizeof(line), fp)) {
        // 移除行尾换行符
        line[strcspn(line, "\n")] = '\0';
        
        if (!first_line) 
        {
            strcat(file_content, "\n"); // 添加选项分隔符
        } else 
        {
            first_line = false;
        }
        
        // 直接使用完整行作为选项
        strcat(file_content, line);
    }
    fclose(fp);
    return true;
}
//获取电影场次
bool read_movie_session(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (!fp) {
        printf("无法打开文件: %s\n", path);
        return false;
    }

    // 清空缓冲区
    memset(file_content, 0, sizeof(file_content));

    char line[128];
    bool first_line = true;

    while (fgets(line, sizeof(line), fp)) {
        // 移除行尾换行符
        line[strcspn(line, "\n")] = '\0';

        if (!first_line) {
            strcat(file_content, "\n"); // 添加选项分隔符
        } else {
            first_line = false;
        }

        strcat(file_content, line); // 添加选项到缓冲区
    }

    fclose(fp);
    return true;
}
//获取选择的座位数量
int get_selected_seat_count(void) 
{
    int count = 0;
    int child_count = lv_obj_get_child_cnt(seats_container);
    
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        if (lv_obj_has_state(seat_btn, LV_STATE_CHECKED)) 
        {
            count++;
        }
    }
    return count;
}


//获取公告信息
char * read_announcement_from_file(const char * filepath) 
{
    FILE *file = fopen(filepath, "r");
    if (!file) 
    {
        // 文件打开失败，返回默认文本
        return "公告文件未找到，使用默认内容";
    }
 
    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    rewind(file);
 
    // 分配内存并读取内容
    char *buffer = (char *)malloc(file_size + 1);
    if (!buffer) 
    {
        fclose(file);
        return "内存分配失败";
    }
 
    fread(buffer, 1, file_size, file);
    buffer[file_size] = '\0';  // 添加字符串结束符
    fclose(file);
 
    // 去除换行符（可选）
    for (int i = 0; i < file_size; i++) 
    {
        if (buffer[i] == '\n') buffer[i] = ' ';
    }
 
    return buffer;
}


// 定义一个滚动动画回调函数
void scroll_anim_cb(void * label, int32_t v) 
{
    // 设置标签的x坐标为v
    lv_obj_set_x((lv_obj_t *)label, v);
}
 
// 动画结束回调：重新开始滚动
void scroll_anim_end_cb(lv_anim_t * anim)
{
    lv_obj_t * label = (lv_obj_t *)anim->var;
    lv_obj_t * cont = lv_obj_get_parent(label);
    
    //重新计算位置时强制布局更新
    lv_obj_update_layout(label);
    lv_obj_update_layout(cont);
    
    //使用相对坐标重置位置
    lv_obj_set_pos(label, 
        lv_obj_get_width(cont),  // 重置到容器右侧
        0
    );
    
    lv_anim_start(anim);
}

//取票按钮事件
void take_ticket_btn_event_cb(lv_event_t * e)
{
    input_ticket_init();
}

//退出取票界面按钮事件
void exit_ticket_btn_event_cb(lv_event_t * e)
{
    lv_obj_clean(ticket);
    login_init();
    lv_scr_load(login);
    lv_obj_del(ticket);
    ticket=NULL;
}

//退出取票输入界面按钮事件
void exit_input_ticket_btn_event_cb(lv_event_t * e)
{
    lv_obj_clean(ticket);
    lv_obj_t * ticket_title = lv_label_create(ticket);//取票标题
    //设置取票标题
    lv_label_set_text(ticket_title, "某某取票系统");
    lv_obj_align(ticket_title, LV_ALIGN_TOP_MID,0,0);
    lv_obj_set_style_text_color(ticket_title, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_text_font(ticket_title, &lv_font_source_han_sans_bold_20, 0);
    //设置取票主窗口
    lv_obj_set_size(ticket, 800, 480);
    lv_obj_set_style_bg_color(ticket, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(ticket, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(ticket, 2, 0);
    lv_obj_set_scroll_dir(ticket, LV_DIR_NONE);
    lv_obj_center(ticket);
    //设置取票界面背景图片
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    //设置取票按钮
    take_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(take_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(take_ticket_btn, LV_ALIGN_CENTER, -50, 0);
    lv_obj_add_event_cb(take_ticket_btn, take_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置取票按钮文本
    lv_obj_t *take_ticket_label = lv_label_create(take_ticket_btn);
    lv_label_set_text(take_ticket_label, "取票");
    lv_obj_center(take_ticket_label);
    lv_obj_set_style_text_font(take_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(take_ticket_label, lv_color_hex(0xffffff), 0);
    //设置买票按钮
    buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(buy_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(buy_ticket_btn, LV_ALIGN_CENTER, 50, 0);
    lv_obj_add_event_cb(buy_ticket_btn, buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置买票按钮文本
    lv_obj_t *buy_ticket_label = lv_label_create(buy_ticket_btn);
    lv_label_set_text(buy_ticket_label, "购票");
    lv_obj_center(buy_ticket_label);
    lv_obj_set_style_text_font(buy_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(buy_ticket_label, lv_color_hex(0xffffff), 0);
    //设置退出按钮
    exit_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_btn, 40, 40);
    lv_style_init(&exit_btn_style);
    lv_style_set_radius(&exit_btn_style, 0);
    lv_style_set_bg_color(&exit_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_btn, &exit_btn_style, 0);
    lv_obj_add_event_cb(exit_btn, exit_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出箭头label
    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_label);

    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);

    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);
    //设置广告
    my_ad = lv_img_create(ticket);
    lv_obj_align(my_ad, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_img_set_src(my_ad, "S:/imgs/guanggao.jpg");
    lv_obj_set_style_opa(my_ad, LV_OPA_70, LV_PART_MAIN);

    //创建容器（背景）
    char *announcement_text = read_announcement_from_file("/notice/set_notice.txt");
    lv_obj_t *cont = lv_obj_create(ticket);
    lv_obj_set_size(cont, 300, 50);
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0x003a57), 0);
    lv_obj_set_style_radius(cont, 5, 0);
    lv_obj_set_style_opa(cont, LV_OPA_70, 0);
    //禁用容器滚动
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE); // 禁止容器自身滚动
    lv_obj_set_scroll_dir(cont, LV_DIR_NONE);        // 禁用滚动条
 
    //创建标签
    lv_obj_t *label_cont = lv_label_create(cont);
    lv_label_set_text(label_cont, announcement_text);
    lv_obj_set_style_text_color(label_cont, lv_color_white(), 0);
    lv_obj_set_style_text_font(label_cont, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_opa(label_cont, LV_OPA_100, 0);
 
    //强制布局更新
    lv_obj_update_layout(label_cont);  // 确保获取正确的对象尺寸
    lv_obj_update_layout(cont);
 
    //相对定位（基于容器坐标系）
    lv_obj_set_pos(label_cont, 
        lv_obj_get_width(cont),        // 初始X位置：容器右侧
        0                              // Y位置：容器顶部
    );
 
    //配置动画
    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_var(&anim, label_cont);
    lv_anim_set_exec_cb(&anim, scroll_anim_cb);
    
    //动态计算滚动距离
    int start_x = lv_obj_get_width(cont);
    int end_x = -lv_obj_get_width(label_cont);
    lv_anim_set_values(&anim, start_x, end_x);
    
    //动画时间与文本长度关联
    int text_width = lv_obj_get_width(label_cont);
    lv_anim_set_time(&anim, 50 * text_width); // 每像素50ms
    
    lv_anim_set_repeat_count(&anim, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_ready_cb(&anim, scroll_anim_end_cb);
    lv_anim_start(&anim);
 
    //延迟释放内存（或使用静态缓冲区）
    if (strcmp(announcement_text, "默认内容") != 0)
    {
        free((void *)announcement_text);
    }
}
//确认取票事件
void confirm_ticket_btn_event_cb(lv_event_t * e)
{
    char selected_ticket[256] = {0};
    lv_dropdown_get_selected_str(movies_dropdown, selected_ticket, sizeof(selected_ticket));

    // 构建用户文件路径
    char user_tickets_path[256];
    snprintf(user_tickets_path, sizeof(user_tickets_path), "/movie_user/%s.txt", as_username);

    // 打开用户票务文件
    FILE *fp = fopen(user_tickets_path, "r");
    if (!fp) {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "无法打开用户票务文件", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    // 创建临时文件
    FILE *temp_fp = fopen("/temp_tickets.txt", "w");
    if (!temp_fp) {
        fclose(fp);
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "无法创建临时文件", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    char line[256];
    bool found = false;
    
    // 逐行处理文件
    while (fgets(line, sizeof(line), fp)) {
        // 移除换行符
        line[strcspn(line, "\n")] = '\0';
        
        // 检查是否匹配选中的票务信息
        if (!found && strcmp(line, selected_ticket) == 0) {
            found = true; // 找到匹配项，跳过写入
            continue;
        }
        
        // 写入未匹配的行到临时文件
        fprintf(temp_fp, "%s\n", line);
    }

    fclose(fp);
    fclose(temp_fp);

    // 替换原始文件
    if (remove(user_tickets_path) != 0) {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "无法删除原文件", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }
    
    if (rename("/temp_tickets.txt", user_tickets_path) != 0) {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "无法重命名临时文件", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    // 解析票务信息用于弹窗
    char movie_name[128], session[128], seat[128], date[128];
    if (sscanf(selected_ticket, "电影名:%127[^,],场次:%127[^,],座位:%127[^,],日期:%127s", 
              movie_name, session, seat, date) == 4) 
    {
        char success_msg[512];
        snprintf(success_msg, sizeof(success_msg), 
                "取票成功！\n电影: %s\n场次: %s\n座位: %s\n日期: %s", 
                movie_name, session, seat, date);
        
        lv_obj_t *success_popup = lv_msgbox_create(ticket, "取票成功", success_msg, NULL, true);
        lv_obj_set_style_text_font(success_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(success_popup, LV_ALIGN_CENTER, 0, 0);
    }
    else 
    {
        // 使用通用成功消息
        lv_obj_t *success_popup = lv_msgbox_create(ticket, "取票成功", "已成功取票！", NULL, true);
        lv_obj_set_style_text_font(success_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(success_popup, LV_ALIGN_CENTER, 0, 0);
    }
}

//确认购票按钮事件
void confirm_buy_ticket_btn_event_cb(lv_event_t * e)
{
    // 获取选中的电影和场次
    char selected_movie[128];
    lv_dropdown_get_selected_str(movies_dropdown, selected_movie, sizeof(selected_movie));
    
    char selected_session[128];
    lv_dropdown_get_selected_str(selectsession_dropdown, selected_session, sizeof(selected_session));
    
    // 保存选中的电影和场次到全局变量
    strncpy(current_selected_movie, selected_movie, sizeof(current_selected_movie) - 1);
    strncpy(current_selected_session, selected_session, sizeof(current_selected_session) - 1);
    
    // 跳转到选座位界面
    seat_selection_init();
}
//选座页面初始化
void seat_selection_init()
{
    lv_obj_clean(ticket);
    
    /* 保持背景 */
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    
    // 设置返回按钮（左上角）
    lv_obj_t *back_btn = lv_btn_create(ticket);
    lv_obj_set_size(back_btn, 40, 40);
    lv_obj_align(back_btn, LV_ALIGN_TOP_LEFT, -10, 10);
    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, LV_SYMBOL_LEFT);
    lv_obj_center(back_label);
    lv_obj_add_event_cb(back_btn, back_to_buy_ticket_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 显示选择的电影和场次
    lv_obj_t *movie_label = lv_label_create(ticket);
    lv_label_set_text_fmt(movie_label, "电影: %s", current_selected_movie);
    lv_obj_align(movie_label, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_set_style_text_font(movie_label, &lv_font_source_han_sans_bold_20, 0);
    
    lv_obj_t *session_label = lv_label_create(ticket);
    lv_label_set_text_fmt(session_label, "场次: %s", current_selected_session);
    lv_obj_align_to(session_label, movie_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_style_text_font(session_label, &lv_font_montserrat_20, 0);
    
    // 创建座位选择区域
    seats_container = lv_obj_create(ticket);
    lv_obj_set_size(seats_container, 600, 300);
    lv_obj_align(seats_container, LV_ALIGN_CENTER, 0, 20);
    lv_obj_set_flex_flow(seats_container, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(seats_container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    
        char filename[256];
    snprintf(filename, sizeof(filename), "/session/%s_%s.txt", 
             current_selected_movie, current_selected_session);
    
    int seat_map[5][11] = {0};  // 默认全可用
    
    // 尝试读取现有座位状态
    FILE *seat_file = fopen(filename, "r");
    if (seat_file) {
        for (int row = 0; row < 5; row++) {
            for (int col = 0; col < 11; col++) {
                fscanf(seat_file, "%d", &seat_map[row][col]);
            }
        }
        fclose(seat_file);
    }

    // 创建座位按钮（5排，每排11个座位）
    for (int row = 1; row <= 5; row++) 
    {
        for (int col = 1; col <= 11; col++) 
        {
            lv_obj_t *seat_btn = lv_btn_create(seats_container);
            lv_obj_set_size(seat_btn, 40, 40);
            
            // 存储座位位置信息
            int *coords = malloc(sizeof(int) * 2);
            coords[0] = row;
            coords[1] = col;
            lv_obj_set_user_data(seat_btn, coords);
            
            // 检查座位状态
            if (seat_map[row-1][col-1] == 1) {
                // 已售座位 - 禁用并设置为灰色
                lv_obj_add_state(seat_btn, LV_STATE_DISABLED);
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0x808080), 0);
            } else {
                // 可用座位 - 设置正常状态
                lv_obj_set_style_bg_color(seat_btn, lv_color_hex(0x0000ff), 0);
                lv_obj_add_event_cb(seat_btn, seat_selected_event_cb, LV_EVENT_CLICKED, NULL);
            }
            
            lv_obj_t *seat_label = lv_label_create(seat_btn);
            lv_label_set_text_fmt(seat_label, "%d-%d", row, col);
            lv_obj_center(seat_label);
        }
    }
    
    // 确认选座按钮
    lv_obj_t *confirm_seat_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(confirm_seat_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(confirm_seat_btn, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_t *confirm_seat_label = lv_label_create(confirm_seat_btn);
    lv_label_set_text(confirm_seat_label, "确认选座");
    lv_obj_center(confirm_seat_label);
    lv_obj_set_style_text_font(confirm_seat_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_add_event_cb(confirm_seat_btn, confirm_seat_selection_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 添加时间和天气
    get_date_str(now_Date, sizeof(now_Date));
    today = lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
    
    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);

    
}

// 返回购票界面事件
void back_to_buy_ticket_event_cb(lv_event_t * e)
{
    // 清理座位按钮的用户数据
    int child_count = lv_obj_get_child_cnt(seats_container);
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        void *data = lv_obj_get_user_data(seat_btn);
        if (data) {
            free(data);
        }
    }

    // 重新初始化购票界面
    buy_ticket_btn_event_cb(NULL);
}

// 座位选择事件
void seat_selected_event_cb(lv_event_t * e)
{
    lv_obj_t * btn = lv_event_get_target(e);
    
    if (lv_obj_has_state(btn, LV_STATE_DISABLED)) {
        return;
    }

    // 切换座位选择状态
    if (lv_obj_has_state(btn, LV_STATE_CHECKED)) {
        lv_obj_clear_state(btn, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x0000ff), 0);
    } else {
        lv_obj_add_state(btn, LV_STATE_CHECKED);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x00ff00), 0);
    }
}


// 确认座位选择事件回调函数
void confirm_seat_selection_event_cb(lv_event_t * e) 
{
    char saved_movie[128] = {0};
    char saved_session[128] = {0};
    char selected_seats[512] = ""; // 存储具体座位信息
    // 获取当前日期
    char date_str[11];
    get_date_str(date_str, sizeof(date_str));


    // 保存电影和场次信息
    strncpy(saved_movie, current_selected_movie, sizeof(saved_movie)-1);
    strncpy(saved_session, current_selected_session, sizeof(saved_session)-1);

    // 收集具体座位信息
    int child_count = lv_obj_get_child_cnt(seats_container);
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        if (lv_obj_has_state(seat_btn, LV_STATE_CHECKED)) 
        {
            int *coords = lv_obj_get_user_data(seat_btn);
            char seat_pos[16];
            snprintf(seat_pos, sizeof(seat_pos), "%d-%d, ", coords[0], coords[1]);
            // 检查字符串长度是否足够
            if (strlen(selected_seats) + strlen(seat_pos) < sizeof(selected_seats)) 
            {
                strcat(selected_seats, seat_pos);
            }
        }
}

// 移除最后一个逗号和空格
    if (strlen(selected_seats) > 0) 
    {
        selected_seats[strlen(selected_seats)-2] = '\0';
    }

    // ---------------------- 输入验证 ----------------------
    // 检查是否已选择电影和场次
    if (current_selected_movie[0] == '\0' || current_selected_session[0] == '\0') {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "请先选择电影和场次！", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    // 获取选中的座位数量
    int seat_count = get_selected_seat_count();
    if (seat_count <= 0) {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "请选择至少一个座位！", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    // ---------------------- 文件路径构造 ----------------------
    char user_tickets_path[256];
    snprintf(user_tickets_path, sizeof(user_tickets_path), "/movie_user/%s.txt", as_username);

    // ---------------------- 写入票务信息 ----------------------
    FILE *fp = fopen(user_tickets_path, "a"); // 追加模式打开用户票务文件
    if (!fp) 
    {
        lv_obj_t *error_popup = lv_msgbox_create(ticket, "错误", "无法保存购票记录！", NULL, true);
        lv_obj_set_style_text_font(error_popup, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
        return;
    }

    // 遍历所有座位，记录选中的座位
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        if (lv_obj_has_state(seat_btn, LV_STATE_CHECKED)) 
        {
            // 获取座位位置
            int *coords = lv_obj_get_user_data(seat_btn);
            int row = coords[0];
            int col = coords[1];
            
            // 写入票务信息：电影名、场次、座位号,日期
            fprintf(fp, "电影名:%s,场次:%s,座位:%d-%d,日期:%s\n", 
                current_selected_movie, 
                current_selected_session, 
                row, col, date_str); 
        }
    }
    
    fclose(fp); // 关闭用户票务文件

    // ---------------------- 更新场次座位状态 ----------------------
    // 生成场次座位文件路径
    char filename[256];
    snprintf(filename, sizeof(filename), "/session/%s_%s.txt", 
             current_selected_movie, current_selected_session);
    
    // 确保session目录存在
    mkdir("/session", 0777);
    
    // 读取或创建座位状态文件
    int seat_map[5][11] = {0};  // 5行11列
    
    FILE *seat_file = fopen(filename, "r");
    if (seat_file) 
    {
        // 读取现有座位状态
        for (int row = 0; row < 5; row++) 
        {
            for (int col = 0; col < 11; col++) 
            {
                fscanf(seat_file, "%d", &seat_map[row][col]);
            }
        }
        fclose(seat_file);
    }
    
    // 更新座位状态
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        if (lv_obj_has_state(seat_btn, LV_STATE_CHECKED)) 
        {
            // 获取座位位置（注意：数组索引从0开始，座位号从1开始）
            int *coords = lv_obj_get_user_data(seat_btn);
            int row = coords[0] - 1;  // 1-5 -> 0-4
            int col = coords[1] - 1;  // 1-11 -> 0-10
            
            // 确保在有效范围内
            if (row >= 0 && row < 5 && col >= 0 && col < 11) {
                seat_map[row][col] = 1; // 标记为已售
            }
        }
    }
    
    // 保存更新后的座位状态
    seat_file = fopen(filename, "w");
    if (seat_file) 
    {
        for (int row = 0; row < 5; row++) 
        {
            for (int col = 0; col < 11; col++) 
            {
                fprintf(seat_file, "%d ", seat_map[row][col]);
            }
            fprintf(seat_file, "\n");
        }
        fclose(seat_file);
    }
    
    // ---------------------- 清理与重置 ----------------------
    // 释放座位按钮的用户数据
    for (int i = 0; i < child_count; i++) 
    {
        lv_obj_t *seat_btn = lv_obj_get_child(seats_container, i);
        void *data = lv_obj_get_user_data(seat_btn);
        if (data) {
            free(data);
        }
    }

    memset(current_selected_movie, 0, sizeof(current_selected_movie));   // 清空电影选择
    memset(current_selected_session, 0, sizeof(current_selected_session)); // 清空场次选择

    // 返回主界面
    lv_obj_clean(ticket);
    lv_obj_t * ticket_title = lv_label_create(ticket);//取票标题
    //设置取票标题
    lv_label_set_text(ticket_title, "某某取票系统");
    lv_obj_align(ticket_title, LV_ALIGN_TOP_MID,0,0);
    lv_obj_set_style_text_color(ticket_title, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_text_font(ticket_title, &lv_font_source_han_sans_bold_20, 0);
    //设置取票主窗口
    lv_obj_set_size(ticket, 800, 480);
    lv_obj_set_style_bg_color(ticket, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(ticket, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(ticket, 2, 0);
    lv_obj_set_scroll_dir(ticket, LV_DIR_NONE);
    lv_obj_center(ticket);
    //设置取票界面背景图片
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    //设置取票按钮
    take_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(take_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(take_ticket_btn, LV_ALIGN_CENTER, -50, 0);
    lv_obj_add_event_cb(take_ticket_btn, take_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置取票按钮文本
    lv_obj_t *take_ticket_label = lv_label_create(take_ticket_btn);
    lv_label_set_text(take_ticket_label, "取票");
    lv_obj_center(take_ticket_label);
    lv_obj_set_style_text_font(take_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(take_ticket_label, lv_color_hex(0xffffff), 0);
    //设置买票按钮
    buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(buy_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(buy_ticket_btn, LV_ALIGN_CENTER, 50, 0);
    lv_obj_add_event_cb(buy_ticket_btn, buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置买票按钮文本
    lv_obj_t *buy_ticket_label = lv_label_create(buy_ticket_btn);
    lv_label_set_text(buy_ticket_label, "购票");
    lv_obj_center(buy_ticket_label);
    lv_obj_set_style_text_font(buy_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(buy_ticket_label, lv_color_hex(0xffffff), 0);
    //设置退出按钮
    exit_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_btn, 40, 40);
    lv_style_init(&exit_btn_style);
    lv_style_set_radius(&exit_btn_style, 0);
    lv_style_set_bg_color(&exit_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_btn, &exit_btn_style, 0);
    lv_obj_add_event_cb(exit_btn, exit_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出箭头label
    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_label);

    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);

    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);
    //设置广告
    my_ad = lv_img_create(ticket);
    lv_obj_align(my_ad, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_img_set_src(my_ad, "S:/imgs/guanggao.jpg");
    lv_obj_set_style_opa(my_ad, LV_OPA_70, LV_PART_MAIN);
    //创建容器（背景）
    char *announcement_text = read_announcement_from_file("/notice/set_notice.txt");
    lv_obj_t *cont = lv_obj_create(ticket);
    lv_obj_set_size(cont, 300, 50);
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0x003a57), 0);
    lv_obj_set_style_radius(cont, 5, 0);
    lv_obj_set_style_opa(cont, LV_OPA_70, 0);
    //禁用容器滚动
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE); // 禁止容器自身滚动
    lv_obj_set_scroll_dir(cont, LV_DIR_NONE);        // 禁用滚动条

    //创建标签
    lv_obj_t *label_cont = lv_label_create(cont);
    lv_label_set_text(label_cont, announcement_text);
    lv_obj_set_style_text_color(label_cont, lv_color_white(), 0);
    lv_obj_set_style_text_font(label_cont, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_opa(label_cont, LV_OPA_100, 0);

    //强制布局更新
    lv_obj_update_layout(label_cont);  // 确保获取正确的对象尺寸
    lv_obj_update_layout(cont);

    //相对定位（基于容器坐标系）
    lv_obj_set_pos(label_cont, 
        lv_obj_get_width(cont),        // 初始X位置：容器右侧
        0                              // Y位置：容器顶部
    );

    //配置动画
    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_var(&anim, label_cont);
    lv_anim_set_exec_cb(&anim, scroll_anim_cb);
    
    //动态计算滚动距离
    int start_x = lv_obj_get_width(cont);
    int end_x = -lv_obj_get_width(label_cont);
    lv_anim_set_values(&anim, start_x, end_x);
    
    //动画时间与文本长度关联
    int text_width = lv_obj_get_width(label_cont);
    lv_anim_set_time(&anim, 50 * text_width); // 每像素50ms
    
    lv_anim_set_repeat_count(&anim, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_ready_cb(&anim, scroll_anim_end_cb);
    lv_anim_start(&anim);

    // ---------------------- 显示成功信息 ----------------------
    char success_msg[256];
    snprintf(success_msg, sizeof(success_msg), 
            "购票成功！\n电影：%s\n场次：%s\n座位数：%d\n日期：%s",
            saved_movie, saved_session, seat_count, date_str);
    
    lv_obj_t *success_popup = lv_msgbox_create(ticket, "购票成功", success_msg, NULL, true);
    lv_obj_set_style_text_font(success_popup, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_align(success_popup, LV_ALIGN_CENTER, 0, 0);

    //延迟释放内存（或使用静态缓冲区）
    if (strcmp(announcement_text, "默认内容") != 0) 
    {
        free((void *)announcement_text);
    }
}

//电影购买下拉列表事件处理函数
void dropdown_event_handler(lv_event_t * e)
{
    lv_obj_t * dropdown = lv_event_get_target(e);
    
    // // 获取选中的文本
    // char buf[128];
    // lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));
    
    // // 获取关联的结果标签
    // lv_obj_t * result_label = lv_obj_get_user_data(dropdown);
    
    // // 更新结果标签
    // char msg[150];
    // snprintf(msg, sizeof(msg), "已选择: %s", buf);
    // lv_label_set_text(result_label, msg);
}

//电影场次选择下拉列表事件处理函数
void selectsession_dropdown_event_cb(lv_event_t * e)
{
    lv_obj_t * dropdown = lv_event_get_target(e);

    // // 获取选中的文本
    // char buf[128];
    // lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));

    // // 获取关联的结果标签
    // lv_obj_t * result_label = lv_obj_get_user_data(dropdown);

    // // 更新结果标签
    // char msg[150];
    // snprintf(msg, sizeof(msg), "已选择: %s", buf);
    // lv_label_set_text(result_label, msg);
}


//退出购票界面按钮事件
void exit_buy_ticket_btn_event_cb(lv_event_t * e)
{
    lv_obj_clean(ticket);
    lv_obj_t * ticket_title = lv_label_create(ticket);//取票标题
    //设置取票标题
    lv_label_set_text(ticket_title, "某某取票系统");
    lv_obj_align(ticket_title, LV_ALIGN_TOP_MID,0,0);
    lv_obj_set_style_text_color(ticket_title, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_text_font(ticket_title, &lv_font_source_han_sans_bold_20, 0);
    //设置取票主窗口
    lv_obj_set_size(ticket, 800, 480);
    lv_obj_set_style_bg_color(ticket, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(ticket, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(ticket, 2, 0);
    lv_obj_set_scroll_dir(ticket, LV_DIR_NONE);
    lv_obj_center(ticket);
    //设置取票界面背景图片
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    //设置取票按钮
    take_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(take_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(take_ticket_btn, LV_ALIGN_CENTER, -50, 0);
    lv_obj_add_event_cb(take_ticket_btn, take_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置取票按钮文本
    lv_obj_t *take_ticket_label = lv_label_create(take_ticket_btn);
    lv_label_set_text(take_ticket_label, "取票");
    lv_obj_center(take_ticket_label);
    lv_obj_set_style_text_font(take_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(take_ticket_label, lv_color_hex(0xffffff), 0);
    //设置买票按钮
    buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(buy_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(buy_ticket_btn, LV_ALIGN_CENTER, 50, 0);
    lv_obj_add_event_cb(buy_ticket_btn, buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置买票按钮文本
    lv_obj_t *buy_ticket_label = lv_label_create(buy_ticket_btn);
    lv_label_set_text(buy_ticket_label, "购票");
    lv_obj_center(buy_ticket_label);
    lv_obj_set_style_text_font(buy_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(buy_ticket_label, lv_color_hex(0xffffff), 0);
    //设置退出按钮
    exit_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_btn, 40, 40);
    lv_style_init(&exit_btn_style);
    lv_style_set_radius(&exit_btn_style, 0);
    lv_style_set_bg_color(&exit_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_btn, &exit_btn_style, 0);
    lv_obj_add_event_cb(exit_btn, exit_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出箭头label
    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_label);
    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);
    //设置广告
    my_ad = lv_img_create(ticket);
    lv_obj_align(my_ad, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_img_set_src(my_ad, "S:/imgs/guanggao.jpg");
    lv_obj_set_style_opa(my_ad, LV_OPA_70, LV_PART_MAIN);
    //创建容器（背景）
    char *announcement_text = read_announcement_from_file("/notice/set_notice.txt");
    lv_obj_t *cont = lv_obj_create(ticket);
    lv_obj_set_size(cont, 300, 50);
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0x003a57), 0);
    lv_obj_set_style_radius(cont, 5, 0);
    lv_obj_set_style_opa(cont, LV_OPA_70, 0);
    //禁用容器滚动
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE); // 禁止容器自身滚动
    lv_obj_set_scroll_dir(cont, LV_DIR_NONE);        // 禁用滚动条
 
    //创建标签
    lv_obj_t *label_cont = lv_label_create(cont);
    lv_label_set_text(label_cont, announcement_text);
    lv_obj_set_style_text_color(label_cont, lv_color_white(), 0);
    lv_obj_set_style_text_font(label_cont, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_opa(label_cont, LV_OPA_100, 0);
 
    //强制布局更新
    lv_obj_update_layout(label_cont);  // 确保获取正确的对象尺寸
    lv_obj_update_layout(cont);
 
    //相对定位（基于容器坐标系）
    lv_obj_set_pos(label_cont, 
        lv_obj_get_width(cont),        // 初始X位置：容器右侧
        0                              // Y位置：容器顶部
    );
 
    //配置动画
    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_var(&anim, label_cont);
    lv_anim_set_exec_cb(&anim, scroll_anim_cb);
    
    //动态计算滚动距离
    int start_x = lv_obj_get_width(cont);
    int end_x = -lv_obj_get_width(label_cont);
    lv_anim_set_values(&anim, start_x, end_x);
    
    //动画时间与文本长度关联
    int text_width = lv_obj_get_width(label_cont);
    lv_anim_set_time(&anim, 50 * text_width); // 每像素50ms
    
    lv_anim_set_repeat_count(&anim, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_ready_cb(&anim, scroll_anim_end_cb);
    lv_anim_start(&anim);
 
    //延迟释放内存（或使用静态缓冲区）
    if (strcmp(announcement_text, "默认内容") != 0) 
    {
        free((void *)announcement_text);
    }
}


//用户取票界面初始化
void input_ticket_init()
{
    lv_obj_clean(ticket);
    /*保持原背景*/
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    /*保持原背景*/

    //设置天气和时间
    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(weather_label, lv_color_hex(0x0000ff), 0);

    //设置退出取票输入界面按钮
    exit_input_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_input_ticket_btn, 40, 40);
    lv_obj_t *exit_input_ticket_label = lv_label_create(exit_input_ticket_btn);
    lv_style_init(&exit_intput_ticket_btn_style);
    lv_style_set_radius(&exit_intput_ticket_btn_style, 0);
    lv_style_set_bg_color(&exit_intput_ticket_btn_style, lv_color_hex(0x2B2B2B));
    lv_obj_add_style(exit_input_ticket_btn, &exit_intput_ticket_btn_style, 0);
    lv_obj_align(exit_input_ticket_btn, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_add_event_cb(exit_input_ticket_btn, exit_input_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出取票输入界面按钮label
    lv_label_set_text(exit_input_ticket_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_input_ticket_label);
    //设置确认取票按钮
    confirm_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(confirm_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(confirm_ticket_btn, LV_ALIGN_CENTER, 0, 100);
    lv_obj_add_event_cb(confirm_ticket_btn, confirm_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置确认取票按钮文本
    lv_obj_t *confirm_ticket_label = lv_label_create(confirm_ticket_btn);
    lv_label_set_text(confirm_ticket_label, "确认取票");
    lv_obj_center(confirm_ticket_label);
    lv_obj_set_style_text_font(confirm_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    //设置用户数据路径
    char file_path[256];
    snprintf(file_path, sizeof(file_path), "/movie_user/%s.txt", as_username);  // 添加.txt扩展名

    // 使用正确的路径读取用户电影票信息
    if (!read_user_movie(file_path)) 
    {
        // 创建错误标签
        lv_obj_t *error_label = lv_label_create(ticket);
        lv_label_set_text(error_label, "无法加载电影列表");
        lv_obj_align(error_label, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_style_text_font(error_label, &lv_font_source_han_sans_bold_20, 0);
        return;
    }
    //设置取票电影下拉框
    movies_dropdown = lv_dropdown_create(ticket);
    lv_dropdown_set_options(movies_dropdown, file_content); // 设置下拉列表的选项
    lv_obj_set_size(movies_dropdown, 650, 50);
    lv_obj_align(movies_dropdown, LV_ALIGN_CENTER, 0, -50);
    lv_obj_set_style_text_font(movies_dropdown, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_bg_color(movies_dropdown, lv_color_hex(0xf0f0f0), 0);
    lv_obj_set_style_border_width(movies_dropdown, 2, 0);
    lv_obj_set_style_border_color(movies_dropdown, lv_color_hex(0x0075ff), 0);
    lv_obj_t * list = lv_dropdown_get_list(movies_dropdown);
    lv_obj_set_style_text_font(list, &lv_font_source_han_sans_bold_20, 0);
    // //设置结果标签
    // lv_obj_t * result_label = lv_label_create(ticket);
    // lv_obj_set_style_text_font(result_label, &lv_font_source_han_sans_bold_20, 0);
    // lv_obj_align(result_label, LV_ALIGN_CENTER, 0, 50);
    // lv_obj_set_user_data(movies_dropdown, result_label); // 关联结果标签

    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
}

//买票界面初始化
void buy_ticket_btn_event_cb(lv_event_t * e)
{
    lv_obj_clean(ticket);
    /*保持原背景*/
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    /*保持原背景*/

    //设置天气和时间
    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);

    //下拉列表设置
    movies_dropdown = lv_dropdown_create(ticket);
    lv_obj_set_size(movies_dropdown, 300, 50);
    lv_obj_align(movies_dropdown, LV_ALIGN_CENTER, 0, -50);
    lv_obj_set_style_text_font(movies_dropdown, &lv_font_source_han_sans_bold_20, 0);
    //选择场次下拉列表设置
    selectsession_dropdown = lv_dropdown_create(ticket);
    lv_obj_set_size(selectsession_dropdown, 300, 50);
    lv_obj_align(selectsession_dropdown, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_text_font(selectsession_dropdown, &lv_font_source_han_sans_bold_20, 0);

    // 读取文件内容
    const char *file_path = "/movies/had_movies.txt";
    if (!read_movie_file(file_path)) 
    {
        // 文件读取失败，创建错误标签
        lv_obj_t * error_label = lv_label_create(ticket);
        lv_label_set_text(error_label, "无法加载电影列表");
        lv_obj_align(error_label, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_style_text_font(error_label, &lv_font_source_han_sans_bold_20, 0);
        return;
    }
    //下拉电影列表设置
    lv_dropdown_set_options(movies_dropdown, file_content); // 设置下拉列表的选项
    lv_obj_set_style_bg_color(movies_dropdown, lv_color_hex(0xf0f0f0), 0);
    lv_obj_set_style_border_width(movies_dropdown, 2, 0);
    lv_obj_set_style_border_color(movies_dropdown, lv_color_hex(0x0075ff), 0);
    lv_obj_set_style_text_font(movies_dropdown, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_t * list = lv_dropdown_get_list(movies_dropdown);
    lv_obj_set_style_text_font(list, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_add_event_cb(movies_dropdown, dropdown_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
    // //设置结果标签
    // lv_obj_t * result_label = lv_label_create(ticket);
    // lv_obj_set_style_text_font(result_label, &lv_font_source_han_sans_bold_20, 0);
    // lv_obj_align(result_label, LV_ALIGN_CENTER, 0, 150);
    // lv_obj_set_user_data(movies_dropdown, result_label); // 关联结果标签

    //选择场次下拉列表设置
    file_path = "/movies/session.txt";
    if(!read_movie_session(file_path))
    {
        lv_obj_t * error_label = lv_label_create(ticket);
        lv_label_set_text(error_label, "无法加载场次列表");
        lv_obj_align(error_label, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_style_text_font(error_label, &lv_font_source_han_sans_bold_20, 0);
        return;
    }
    lv_dropdown_set_options(selectsession_dropdown, file_content); // 设置下拉列表的选项
    lv_obj_set_style_bg_color(selectsession_dropdown, lv_color_hex(0xf0f0f0), 0);
    lv_obj_set_style_border_width(selectsession_dropdown, 2, 0);
    lv_obj_set_style_border_color(selectsession_dropdown, lv_color_hex(0x0075ff), 0);
    lv_obj_set_style_text_font(selectsession_dropdown, &lv_font_montserrat_20, 0);
    lv_obj_t * list2 = lv_dropdown_get_list(selectsession_dropdown);
    lv_obj_set_style_text_font(list2, &lv_font_montserrat_20, 0);
    lv_obj_add_event_cb(selectsession_dropdown, selectsession_dropdown_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    //设置电影场次结果标签
    // lv_obj_t * result_label2 = lv_label_create(ticket);
    // lv_obj_set_style_text_font(result_label2, &lv_font_montserrat_20, 0);
    // lv_obj_align(result_label2, LV_ALIGN_CENTER, 0, 200);
    // lv_obj_set_user_data(selectsession_dropdown, result_label2); // 关联结果标签

    //设置退出买票界面按钮
    exit_buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_buy_ticket_btn, 40, 40);
    lv_style_init(&exit_buy_ticket_btn_style);
    lv_style_set_radius(&exit_buy_ticket_btn_style, 0);
    lv_style_set_bg_color(&exit_buy_ticket_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_buy_ticket_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_buy_ticket_btn, &exit_buy_ticket_btn_style, 0);
    lv_obj_align(exit_buy_ticket_btn, LV_ALIGN_TOP_LEFT, 0, 0);
    //设置退出购票按钮label
    lv_obj_t * exit_buy_ticket_label = lv_label_create(exit_buy_ticket_btn);
    lv_label_set_text(exit_buy_ticket_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_buy_ticket_label);
    //设置退出买票界面按钮事件
    lv_obj_add_event_cb(exit_buy_ticket_btn, exit_buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置确认购买按钮
    confirm_buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(confirm_buy_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(confirm_buy_ticket_btn, LV_ALIGN_CENTER, 0, 100);
    lv_obj_add_event_cb(confirm_buy_ticket_btn, confirm_buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置确认购买按钮label
    lv_obj_t * confirm_buy_ticket_label = lv_label_create(confirm_buy_ticket_btn);
    lv_label_set_text(confirm_buy_ticket_label, "购买");
    lv_obj_center(confirm_buy_ticket_label);
    lv_obj_set_style_text_font(confirm_buy_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    
    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
}


//取票界面初始化
void ticket_init(void)
{
    ticket=lv_obj_create(NULL);//取票主窗口
    /*界面切换以及内存处理*/
    lv_scr_load(ticket);
    lv_obj_del(login);
    login=NULL;
    /*界面切换以及内存处理*/
    lv_obj_t * ticket_title = lv_label_create(ticket);//取票标题
    //设置取票标题
    lv_label_set_text(ticket_title, "某某取票系统");
    lv_obj_align(ticket_title, LV_ALIGN_TOP_MID,0,0);
    lv_obj_set_style_text_color(ticket_title, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_text_font(ticket_title, &lv_font_source_han_sans_bold_20, 0);
    //设置天气和时间
    weather_label = lv_label_create(ticket);
    lv_label_set_text(weather_label, "天气  多云");
    lv_obj_align(weather_label, LV_ALIGN_TOP_RIGHT, -20, 0);
    lv_obj_set_style_text_font(weather_label, &lv_font_source_han_sans_bold_20, 0);

    //设置取票主窗口
    lv_obj_set_size(ticket, 800, 480);
    lv_obj_set_style_bg_color(ticket, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(ticket, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(ticket, 2, 0);
    lv_obj_set_scroll_dir(ticket, LV_DIR_NONE);
    lv_obj_center(ticket);
    //设置取票界面背景图片
    jpg_bg = lv_img_create(ticket);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    //设置取票按钮
    take_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(take_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(take_ticket_btn, LV_ALIGN_CENTER, -50, 0);
    lv_obj_add_event_cb(take_ticket_btn, take_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置取票按钮文本
    lv_obj_t *take_ticket_label = lv_label_create(take_ticket_btn);
    lv_label_set_text(take_ticket_label, "取票");
    lv_obj_center(take_ticket_label);
    lv_obj_set_style_text_font(take_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(take_ticket_label, lv_color_hex(0xffffff), 0);
    //设置买票按钮
    buy_ticket_btn = lv_btn_create(ticket);
    lv_obj_set_style_bg_color(buy_ticket_btn, lv_color_hex(0x0000ff), 0);
    lv_obj_align(buy_ticket_btn, LV_ALIGN_CENTER, 50, 0);
    lv_obj_add_event_cb(buy_ticket_btn, buy_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置买票按钮文本
    lv_obj_t *buy_ticket_label = lv_label_create(buy_ticket_btn);
    lv_label_set_text(buy_ticket_label, "购票");
    lv_obj_center(buy_ticket_label);
    lv_obj_set_style_text_font(buy_ticket_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(buy_ticket_label, lv_color_hex(0xffffff), 0);
    //设置退出按钮
    exit_btn = lv_btn_create(ticket);
    lv_obj_set_size(exit_btn, 40, 40);
    lv_style_init(&exit_btn_style);
    lv_style_set_radius(&exit_btn_style, 0);
    lv_style_set_bg_color(&exit_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_btn, &exit_btn_style, 0);
    lv_obj_add_event_cb(exit_btn, exit_ticket_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出箭头label
    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_label);
    //登陆成功窗口设置
    ticket_popup= lv_msgbox_create(ticket, "登录成功", "欢迎使用某某取票系统", NULL, true);
    lv_obj_align(ticket_popup, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(ticket_popup, NULL, LV_EVENT_ALL, NULL);
    lv_obj_set_style_text_font(ticket_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_t *test = lv_label_create(lv_scr_act());
    lv_label_set_text(test, "test");
    lv_obj_align(test, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_text_font(test, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(test, lv_color_hex(0xff00f00), 0);
    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(ticket);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
    //设置广告
    my_ad = lv_img_create(ticket);
    lv_obj_align(my_ad, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_img_set_src(my_ad, "S:/imgs/guanggao.jpg");
    lv_obj_set_style_opa(my_ad, LV_OPA_70, LV_PART_MAIN);
    //创建容器（背景）
    char *announcement_text = read_announcement_from_file("/notice/set_notice.txt");
    lv_obj_t *cont = lv_obj_create(ticket);
    lv_obj_set_size(cont, 300, 50);
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_set_style_bg_color(cont, lv_color_hex(0x003a57), 0);
    lv_obj_set_style_radius(cont, 5, 0);
    lv_obj_set_style_opa(cont, LV_OPA_70, 0);
    //禁用容器滚动
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE); // 禁止容器自身滚动
    lv_obj_set_scroll_dir(cont, LV_DIR_NONE);        // 禁用滚动条
 
    //创建标签
    lv_obj_t *label_cont = lv_label_create(cont);
    lv_label_set_text(label_cont, announcement_text);
    lv_obj_set_style_text_color(label_cont, lv_color_white(), 0);
    lv_obj_set_style_text_font(label_cont, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_opa(label_cont, LV_OPA_100, 0);
 
    //强制布局更新
    lv_obj_update_layout(label_cont);  // 确保获取正确的对象尺寸
    lv_obj_update_layout(cont);
 
    //相对定位（基于容器坐标系）
    lv_obj_set_pos(label_cont, 
        lv_obj_get_width(cont),        // 初始X位置：容器右侧
        0                              // Y位置：容器顶部
    );
 
    //配置动画
    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_var(&anim, label_cont);
    lv_anim_set_exec_cb(&anim, scroll_anim_cb);
    
    //动态计算滚动距离
    int start_x = lv_obj_get_width(cont);
    int end_x = -lv_obj_get_width(label_cont);
    lv_anim_set_values(&anim, start_x, end_x);
    
    //动画时间与文本长度关联
    int text_width = lv_obj_get_width(label_cont);
    lv_anim_set_time(&anim, 50 * text_width); // 每像素50ms
    
    lv_anim_set_repeat_count(&anim, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_ready_cb(&anim, scroll_anim_end_cb);
    lv_anim_start(&anim);
 
    //延迟释放内存（或使用静态缓冲区）
    if (strcmp(announcement_text, "默认内容") != 0) 
    {
        free((void *)announcement_text);
    }
}